package com.spider.silence.worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.spider.silence.downloader.ChengduLifeDownLoader;
import com.spider.silence.output.*;
import com.spider.silence.pool.ChengduLifeDownLoaderPool;
import com.spider.silence.task.ChengduLifeTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.DateUtils;

import java.io.File;
import java.util.Map;

/**
 * Created by qiuhao on 2017/1/16.
 */
public class ChengduLifeWorker implements  Runnable {


    @Override
    public void run() {
        while(true && !Thread.interrupted()){
            ChengduLifeDownLoader downLoader=null;
            ChengduLifeDownLoaderPool downLoaderPool=null;
            FileWriter writer = null;
            FileWriterPool writerPool =FileWriterPool.getInstance();
            try {
                downLoaderPool=ChengduLifeDownLoaderPool.getInstance();
                downLoader=(ChengduLifeDownLoader)downLoaderPool.fetchDownLoader(1000);
                if(null!=downLoader){
                   ChengduLifeTask task=(ChengduLifeTask)CommonTaskManager.takeTask("ChengDuLife",1000);
                   if(task==null){
                        continue;
                    }
                   JSONObject content=downLoader.crawlItem(task.getForumUrl());
                   String filePrefix="ChengDuLife";//文件前缀
                    JSONObject data=content.getJSONObject("data");
                    if(null!=data&&!"".equals(data)&&data.size()>0){
                        String time=data.getString("publish_time");
                        long tsp= DateUtils.dateToTimestamp( DateUtils.parseDateTime(time)).getTime();
                        String user_id=data.getString("publish_user_id");
                        String tid=data.getString("id");
                        //writer= FTPWriterPool.getInstance().fetchWriter();
                        writer=writerPool.fetchWriter();
                        String fileName="";
                        if(user_id.equals("") || null==user_id){
                            fileName=filePrefix+"_"+tsp+"_"+tid;//平台名称（或者英文）_ 言论发布的时间戳 _ 发布用户的ID _ 言论的tid
                        }else{
                            fileName=filePrefix+"_"+tsp+"_"+user_id+"_"+tid;//平台名称（或者英文）_ 言论发布的时间戳 _ 发布用户的ID _ 言论的tid
                        }

                        String publishDate=DateUtils.formatDate(DateUtils.parseDateTime(data.getString("publish_time"))).replace("-","-");
                        Map<String,File> map=downLoader.getFiles();
                        writer.write("成都生活论坛_speeches_"+publishDate,fileName,JSON.toJSONString(content,SerializerFeature.BrowserCompatible),map);
                    }
                   System.out.println(JSON.toJSONString(content, SerializerFeature.BrowserCompatible));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(null!=downLoader){
                    downLoaderPool.releaseDownLoader(downLoader);
                }
                if(null!=writer){
                    writerPool.releaseWriter(writer);
                }
            }
        }
    }
}
